package cn.edu.sgu.www.mhxysy.controller.zuoqi;

import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.dto.zuoqi.ZuoqiCategoryDTO;
import cn.edu.sgu.www.mhxysy.entity.zuoqi.ZuoqiCategory;
import cn.edu.sgu.www.mhxysy.pager.zuoqi.ZuoqiCategoryPager;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.zuoqi.ZuoqiCategoryService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "坐骑造型管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/zuoqi_category", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class ZuoqiCategoryController {

	private final ZuoqiCategoryService zuoqiCategoryService;

	@Autowired
	public ZuoqiCategoryController(ZuoqiCategoryService zuoqiCategoryService) {
		this.zuoqiCategoryService = zuoqiCategoryService;
	}

	@ApiOperation("上传图片")
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public JsonResult<String> upload(MultipartFile file) throws IOException {
		String url = zuoqiCategoryService.upload(file);

		return JsonResult.success(CustomMessages.UPLOAD_SUCCESS, url);
	}

	@ApiOperation("添加坐骑造型")
	@ApiOperationSupport(ignoreParameters = "id")
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public JsonResult<Void> insert(@Validated ZuoqiCategoryDTO zuoqiCategoryDTO) {
		zuoqiCategoryService.insert(zuoqiCategoryDTO);

		return JsonResult.success(CustomMessages.INSERT_SUCCESS);
	}

	@ApiOperation("通过ID删除坐骑造型")
	@RequestMapping(value = "/deleteById", method = RequestMethod.POST)
	public JsonResult<Void> deleteById(
			@ApiParam(name = "id", value = "坐骑造型ID", required = true)
			@NotNull(message = CustomMessages.ID_NOT_NULL) Integer id) {
		zuoqiCategoryService.deleteById(id);

		return JsonResult.success(CustomMessages.DELETE_SUCCESS);
	}

	@ApiOperation("通过ID修改坐骑造型信息")
	@RequestMapping(value = "/updateById", method = RequestMethod.POST)
	public JsonResult<Void> updateById(@Validated ZuoqiCategoryDTO zuoqiCategoryDTO) {
		zuoqiCategoryService.updateById(zuoqiCategoryDTO);

		return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
	}

	@ApiOperation("通过ID修改坐骑图片")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "坐骑造型ID", required = true),
			@ApiImplicitParam(name = "image", value = "图片地址", required = true)
	})
	@RequestMapping(value = "/updateImageById", method = RequestMethod.POST)
	public JsonResult<Void> updateImageById(
			@NotNull(message = CustomMessages.ID_NOT_NULL) Integer id,
			@NotBlank(message = CustomMessages.IMAGE_NOT_NULL) String image) {
		zuoqiCategoryService.updateImageById(id, image);

		return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
	}

	@ApiOperation("查询全部坐骑造型")
	@RequestMapping(value = "/selectAll", method = RequestMethod.GET)
	public JsonResult<List<ZuoqiCategory>> selectAll() {
		List<ZuoqiCategory> list = zuoqiCategoryService.selectAll();

		return JsonResult.success(null, list);
	}

	@ApiOperation("通过ID查询坐骑造型信息")
	@RequestMapping(value = "/selectById", method = RequestMethod.GET)
	public JsonResult<ZuoqiCategory> selectById(
			@ApiParam(name = "id", value = "坐骑造型ID", required = true)
			@NotNull(message = CustomMessages.ID_NOT_NULL) Integer id) {
		ZuoqiCategory zuoqiCategory = zuoqiCategoryService.selectById(id);

		return JsonResult.success(null, zuoqiCategory);
	}

	@ApiOperation("分页查询坐骑造型列表")
	@RequestMapping(value = "/selectByPage", method = RequestMethod.GET)
	public JsonResult<JsonPage<ZuoqiCategory>> selectByPage(ZuoqiCategoryPager pager) {
		Page<ZuoqiCategory> page = zuoqiCategoryService.selectByPage(pager);

		return JsonResult.restPage(page);
	}

}